/**
 *给你单链表的头指针 head 和两个整数 left 和 right ，其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。
 *https://leetcode.cn/problems/reverse-linked-list-ii/description/
 */
class ReverseBetween {
    ListNode successor = null;
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if(left == 1) {
           return reverseN(head,right);//反转前n个结点
        }
        head.next = reverseBetween(head.next,left-1,right-1);
        return head;
    }

    public ListNode reverseN(ListNode head,int n) {
        if(n==1) {
            successor = head.next;
            return head;
        }
        ListNode last = reverseN(head.next,n-1);
        head.next.next = head;
        head.next = successor;
        return last;
    }

}